Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java Control Statements

    From top to bottom, the code is executed by the Java compiler. The order in which the statements appear in the code determines how they are executed. To control the flow of Java code, however, statements are offered by Java. Control flow statements are what these sentences are known as. It is one of the core characteristics of Java that ensures a seamless flow of programs.



Table Of Content

  • Java Control Statements
  • Type Of Control Statement
  • Decision Making statements
  • Loop statements
  • Jump statements

Type Of Control Statement

1. Decision Making statements

  • if statements
  • switch statement

2. Loop statements

  • for loop
  • while loop
  • do while loop
  • for-each loop

3. Jump statements

  • break statement
  • continue statement


Decision-Making statements

If..else statement


In this statement, the if block is only performed if the provided condition is true. If not, the else block is carried out.


Let's look at the basic java contol statement example
 // Decision-Making program using if...else  
 import java.io.*;
 public class Control{  
public static void main(String args[ ]){
int x =  8; 
if  (x>10)   
System.out.println("X is greater than 10");   
else  
System.out.println("X is less  than 10"); 
 }
}

Output:

X is less  than 10 

The four different kinds of if-statements are listed below.

  • Normal if statement
  • if-else statement
  • if-else if statement
  • Nested if-statement

Let's look at each of the if statements one by one.


1.Normal if statement


Syntax

 if (condition) {  
     statement 1;  // your code  execute when condition is true 
  } 



 // Decision-Making program using if 
 import java.io.*;
 public class Control{  
public static void main(String args[ ]){
int x =  7; 
int y =  4; 
if (x-y>2)   
System.out.println( "x-y is greater than 2"); 
 }
}

Output:

x-y is greater than 2 

2. if-else statement


Syntax

 if (condition) {  
     statement 1;  // your code  execute when condition is true 
  } 
 else {  
     statement 2;  // your code  execute when condition is false 
  } 

 // Decision-Making program using if...else  
 import java.io.*;
 public class Control{  
public static void main(String args[ ]){
int x =  8; 
int y =  10; 
if (x-y>5)   
System.out.println( "x-y is greater than 5"); 
else  
System.out.println("X is less  than 5"); 
 }
}

Output:

X is less  than 5 

Tip

if one statement is inside if and else no need to open curley barcket if there is multiple statements then you must use curley bracket to execute that block of code inside it.

Let's see one example

 // Decision-Making program using if...else  
 import java.io.*;
 public class Control{  
public static void main(String args[ ]){
int x =  11; 
if  (x==10)   
System.out.println("x is equal to 10");    //only one statement inside  if   

if  (x>10) {   
System.out.println("x is greater than 10");  //multiple statement inside if      
System.out.println("value of x is "+x);   
 }
else  
System.out.println("X is less  than 10"); 
 }
}

Output:

x is greater than 10 
value of x is 11 

3. if-else-if statement

The if-statement is followed by several else-if statements in an if-else-if statement. In other terms, we may say that a decision tree is created by the series of if-else statements, and the computer can enter the block of code when the condition is true. At the last, we may also define an else statement.

Syntax

 if (condition1) {  
     statement 1;  // your code  execute when condition 1 is true 
  } 
 else if (condition2) {  
     statement 2;  // your code  execute when condition 2 is true 
  } 
 else {  
     statement 3;  // your code  execute when condition is false 
  } 


 // Decision-Making program using if - else if  
 import java.io.*;
 public class Control{  
public static void main(String args[ ]){
String Country =  "India"; 
if (Country== "USA") {   
System.out.println("Welcome to USA");   
 }
else if  (Country=="India") {   
System.out.println("Welcome to India");   
 }
else if  (Country=="Japan") {   
System.out.println("Welcome to Japan");   
 }
else  
System.out.println("Welcome to"+Country); 
 }
}

Output:

Welcome to India 

4. Nested if-statement


If or if-else statements may be contained within another if or else-if statement it is called nested if-statements.

Syntax

 if (condition1) {  
 if (condition1)   {   
     statement 1;  // your code  execute when condition 2 is true 
  } 
 else {  
     statement 2;  // your code  execute when condition is false 
  } 
  } 

 // Decision-Making program using nested if - else if  
   class Student
    {
        public static void main(String args[])
        {
            int a=5;
            int b=10;
        
            if(a==5)
            {
                if(b==10)
                {
                    System.out.println("Welcome to my website");
                }
                else 
                {
                    System.out.println("Sorry value of b is not 10");
                }
            }
        }
    }
    

Output:

Welcome to my website 
Decision-Making statements

Switch statement

  • Switch statements are used in Java to enable the execution of a single statement in many circumstances. The switch statement may be used with variables of various types, including short, byte, int, long, enum, and others.
  • You have the option to give one or N case values for a switch expression.
  • The break statement is used to stop the chain of statements. It's optional to use this expression. If this statement is omitted, the subsequent occurrence will be handled.
  • Similar case values are not permitted. The compiler generates a compile-time error if unique values are not used.

Syntax

 switch  (expression){  
 case  value1: 
     statement 1;  // your code  execute when condition 2 is true 
     break;   
 case  value2: 
     statement 2;  // your code  execute when condition 2 is true 
     break;   
  default:   
     statement 2;  // your code  execute when condition is false 
  } 
  } 

 // Decision-Making program using switch case statement  
public class Student {  
    public static void main(String[] args) {  
        int days = 5;
        switch (days) {
          case 1:
            System.out.println("Monday");
            break;
          case 2:
            System.out.println("Tuesday");
            break;
          case 3:
            System.out.println("Wednesday");
            break;
          case 4:
            System.out.println("Thursday");
            break;
          case 5:
            System.out.println("Friday");
            break;
          case 6:
            System.out.println("Saturday");
            break;
          case 7:
            System.out.println("Sunday");
            break;
         }  
      }  
    }  
    

Output:

Friday 
Loop Statement:

for loop statement

With only one line of code, we can initialise the loop variable, verify the condition, and increment or decrement. Only when we are certain of the number of times we want to run the block of code do we utilise the for loop.

Syntax

 for  (initialization, condition, increment/decrement) { 
 // block of statement 
  } 


 //  program using java for loop  
   public class Student {  
	public static void main(String[] args) {  
        int sum = 0;  
            for(int j = 0; j<=100; j++) {  
            sum = sum + j;  
            }  
        System.out.println("The sum of first 100 whole numbers is " + sum);  
        }  
    }  
    

Output:

The sum of first 100 whole numbers is 5050 

for each loop statement

With only one line of code, we can initialise the loop variable, verify the condition, and increment or decrement. Only when we are certain of the number of times we want to run the block of code do we utilise the for loop.

Syntax

 for  (String string : args) {
 // block of statement 
  } 

 // Java Loop program using for-each loop  
   public class Student {  
	public static void main(String[] args) {  
        String[] Country = {"India","USA","Canada","Japan","SriLanka"};    
        for(String CountryList:Country) {    
            System.out.println(CountryList);    
        }    	
      }  
    } 
    

Output:

India 
USA 
Canada 
Japan 
SriLanka 

Java while loop

The while loop is also used to repeatedly loop through the number of statements. However, it is advised to use a while loop if we are unsure of the number of iterations. In contrast to for loop, while loop's initialization and increment/decrement operations do not happen inside the while loop statement.

Since the condition is verified at the beginning of the loop, it is also referred to as the entry-controlled loop. If the condition is satisfied, the body of the loop will be run; otherwise, the statements following the loop will be run.

Syntax

 while  (condition) {
 // block of statement 
  } 

 // Java Loop program using while loop  
public class Student {  
	public static void main(String[] args) {  
		int i = 1;    
		System.out.println("Print the first 10 natural numbers ");    
		while(i<=10) {    
		System.out.println(i);    
		i = i + 1;    
		}    
       }  
}  

Output:

Print the first 10 natural numbers 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 

Java do while loop

A block of statements is constantly run until the specified condition is true using a Java do-while loop. The Java do-while loop is similar to the while loop with the exception that the condition is verified after the statements have been executed. do..while loop will ensure that statement inside loop will execute once before condition verification

Syntax

 do {
 // block of statement 
  }while(condition); 

 // Java Loop program using do.. while loop  
public class Student {  
	public static void main(String[] args) {  
		int i = 1;      
		System.out.println("Printing the list of first 10 odd numbers");    
		do {    
		System.out.println(i);    
		i = i + 2;    
		}while(i<=10);    
       }  
}  

Output:

Printing the list of first 10 odd numbers  
1 
3 
5 
7 
9 

Jump Statements:

Jump statements are control statements that move programme execution from one location execution flow to another. In the Java programming language, there are two Jump statements available. i.e break & continue


1. Break statement.

2. Continue statement


Break statement

The break statement in Java is used to stop the execution of the looping or switch statement that is closest. With the switch statement, for loop, while loop, and do-while loop, the break statement is frequently used.

Syntax

 break; 

When a loop encounters a break statement, the loop is broken, and control shifts to the sentence that follows the loop.

 // break statement inside loop  
public class Student {  
	public static void main(String[] args) {  
		for(int i = 1; i<= 20; i++) {  
			System.out.println(i);  
			if(i==9) {  
			break;    // loop  will stop at 9
			}  
		}     
       }  
} 

Output:

1  
2 
3 
4 
5 
6 
7 
8 
9 


Java continue statement

The continue statement skips any code in between itself and the conditional expression that governs the loop, causing the next iteration of the loop to execute.

Syntax

 continue; 

When a loop encounters a continue statement, that iteration is skip in loop

 // continue statement inside loop  
public class Student {  
	public static void main(String[] args) {  
        for (int i = 1; i < 15; i++) {
            if (i == 9){
            	 System.out.println();     // this print statement to just show skip value   
                  continue; // use continue keyword to skip the current iteration
            }
            System.out.println(i);
        }     
    }  
} 

Output:

1  
2 
3 
4 
5 
6 
7 
8 
  
10 
11 
12 
13 
14 
java if else Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Control Statements
java if else
java switch
java for loop
java While loop
java Do While loop
java break
java continue
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.